<?php
/*------------------------------------------
* @AUTHOR: TANDAT @EMAIL:tidusvn05@gmail.com
* @PHONE: +84933731173
* -----------------------------------------*/

/*------------------------------------------
* 1 crawler de get data
* 
* -----------------------------------------*/
class Crawler extends D_Cassandra_Abstract {
	
	//auto crawler cassandra
	function autocrawler(){
		$data = array(
				'name'=>'Crawler '.Time::now(),
				'started_at' => Time::now(),
				'depth' => 1,
		);
		
		//asign keywords for crawler
		$kw_obj = new Keyword();
		$keywords = $kw_obj->all_keys();
		//asign website for crawler
		$ws_obj = new Website();
		$websites = $ws_obj->all_keys();
		
		$data['websites'] = serialize($websites);
		$data['keywords'] = serialize($keywords);
		$data['current_depth'] = 1;
		
		$crawler_id = $this->count() + 1;
		$this->insert($crawler_id, $data);
		
		return $crawler_id;
	}
	
	
	
	
	
	// old function
	
	//auto crawler
	//create auto assign website and keywords
	function autocrawlerxx(){
		$data = array(
				'name'=>'Crawler '.Time::now(),
				'started_at' => Time::now(),
				'depth' => 1,
				);
		$crawler_id = $this->insert($data);
		//asign keywords for crawler
		$kw_obj = new Keyword();
		$cwl_kw_obj = new CrawlerKeyword();
		$keywords = $kw_obj->listkeyword();
		foreach ($keywords as $keyword){
			$data = array(
					'crawler_id' => $crawler_id, 
					'keyword_id' => $keyword->id,
					);
			$cwl_kw_obj->insert($data);
		}
		
		//asign website for crawler
		$ws_obj = new Website();
		$cwl_ws_obj = new CrawlerWebsite();
		$websites = $ws_obj->listwebsite();
		foreach ($websites as $website){
			$data = array(
					'crawler_id' => $crawler_id,
					'website_id' => $website->id,
			);
			$cwl_ws_obj->insert($data);
		}
		
		return $crawler_id;
	}
	
	//get all website 
	public function get_websites_by_id($crawler_id){
		$sql = $this->select()->setIntegrityCheck(false)
		->from(array('c' => $this->_name), array('*'))
		->join(array('cw' => 'crawlers_websites'), 'cw.crawler_id = c.id', array('*'))
		->join(array('w' => 'websites'), 'cw.website_id = w.id', array('url','name'))
		->where('c.id = ?', $crawler_id)
		;
		return $this->fetchAll($sql);
	}
	
	//get all website not crawled
	public function list_website($crawler_id){
		$sql = $this->select()->setIntegrityCheck(false)
			->from(array('c' => $this->_name), array('*'))
			->join(array('cw' => 'crawlers_websites'), 'cw.crawler_id = c.id', array('*'))
			->join(array('w' => 'websites'), 'cw.website_id = w.id', array('url','name'))
			->where('cw.id > c.current_website_id')
			->where('c.id = ?', $crawler_id)
		;
		return $this->fetchAll($sql);
	}
	
	//get all website not crawled
	public function list_keyword($crawler_id){
		$sql = $this->select()->setIntegrityCheck(false)
		->from(array('c' => $this->_name), null)
		->join(array('cw' => 'crawlers_keywords'), 'cw.crawler_id = c.id', null)
		->join(array('k' => 'keywords'), 'cw.keyword_id = k.id', array('id','name'))
		->where('c.id = ?', $crawler_id)
		;
		return $this->fetchAll($sql);
	}
	
	//start
	public function start($id){
		$data = array(
					'started_at' => Time::now()
				);
		return $this->update($data, "id=".$id);
	}
	//finish
	public function finish($id){
		$data = array(
					'finished_at' => Time::now()
				);
		return $this->update($data, "id=".$id);
	}
	
	public function update_current_website($id, $website_id){
		$data = array(
					'current_website_id' => $website_id
				);
		$where = array('id' => $id);
		return $this->update($data, $where);
	}
	
	public function update_current_depth($id, $depth){
		$data = array(
				'current_depth' => $depth
		);
		$where = array('id' => $id);
		return $this->update($data, $where);
	}
	
	//update current url of website
	public function update_current_url($id, $url_id){
		$data = array(
				'current_url_id' => $url_id
		);
		$where = array('id' => $id);
		return $this->update($data, $where);
	}
	
	//assign website for crawler
	function assign_website($crawler_id, $website_ids){
		$crawler_website_obj = new CrawlerWebsite();
		foreach($website_ids as $website_id){
			$data = array(
					"website_id" =>$website_id,
					"crawler_id" => $crawler_id,
					);
			$check = $crawler_website_obj->fetchOne($data);
			if($check){
				
			}else{
				$crawler_website_obj->insert($data);
			}
		}
		return true;
	}
	
}

?>